package com.cnswhy.cloud.server.app.controller.iot;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cnswhy.cloud.common.core.exception.BizException;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.server.app.controller.BaseController;
import com.cnswhy.cloud.server.app.dto.IotChargingLineLinkMerchantDTO;
import com.cnswhy.cloud.server.app.dto.IotChargingLineLinkPublicMerchantDTO;
import com.cnswhy.cloud.server.app.dto.IotTerminalAddDTO;
import com.cnswhy.cloud.server.app.dto.TerminalSummeyDto;
import com.cnswhy.cloud.server.common.entity.enumeration.IotType;
import com.cnswhy.cloud.server.core.entity.*;
import com.cnswhy.cloud.server.core.service.*;
import com.cnswhy.cloud.server.core.service.customer.CustomerService;
import com.cnswhy.cloud.server.core.service.merchant.MerchantService;
import com.cnswhy.cloud.server.core.vo.RespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "终端管理")
@Controller
@RequestMapping("/iot/term")
@Slf4j
public class IotTerminalController extends BaseController {
    @Autowired
    IotTerminalInfoService iotTerminalInfoService;
    @Autowired
    IotChargingLineScaleService iotChargingLineScaleService;
    @Autowired
    MerchantService merchantService;
    @Autowired
    CustomerService customerService;
    @Autowired
    IotChargingLineTransOrderService iotChargingLineTransOrderService;
    @Autowired
    SplitTransRecordService splitTransRecordService;
    @Autowired
    IotChargingLineInfoService iotChargingLineInfoService;

    @ApiOperation(value = "查询设备状态", httpMethod = "POST")
    @RequestMapping(value = "/queryTerminalId", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody

    public RespVo queryTerminalId(HttpServletRequest request, IotTerminalAddDTO iotTerminalAddDTO) {
        try{
            Customer customer = getLoginCustomer(request);



            return RespVo.success(iotTerminalInfoService.queryTerminal(iotTerminalAddDTO.getIotType(),iotTerminalAddDTO.getTerminalId(),customer.getId()));
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("查询设备状态");
        }
    }

    @ApiOperation(value = "查看设备订单列表", httpMethod = "GET")
    @RequestMapping(value = "/queryTerminalOrder", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo queryTerminalOrder(HttpServletRequest request,  @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, IotTerminalAddDTO iotTerminalAddDTO) {
        try{
            Customer customer = getLoginCustomer(request);
            if(IotType.IOT_CHARG_LINE.eq(iotTerminalAddDTO.getIotType())){
                Page<IotChargingLineTransOrder> page = new Page<>(currentPage, pageSize);
                IPage<IotChargingLineTransOrder> pageList = iotChargingLineTransOrderService.page(page,
                        Wraps.<IotChargingLineTransOrder>lbQ().eq(IotChargingLineTransOrder::getTerminalId,iotTerminalAddDTO.getTerminalId())
                                .eq(IotChargingLineTransOrder::getStatus,2).orderByDesc(IotChargingLineTransOrder::getId));
                for(IotChargingLineTransOrder iotChargingLineTransOrder : pageList.getRecords()){
                    SplitTransRecord splitTransRecord = splitTransRecordService.getOne(
                            Wraps.<SplitTransRecord>lbQ().eq(SplitTransRecord::getOrderId,iotChargingLineTransOrder.getOrderId())
                            .eq(SplitTransRecord::getSplitCustomerId,customer.getId()));
                    if(splitTransRecord != null){
                        iotChargingLineTransOrder.setSplitTransRecord(splitTransRecord);
                    }
                }
                return RespVo.success(pageList);
            }   
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return RespVo.fail("查询设备收费列表失败");
        }
        return RespVo.fail("物联设备类型不可为空");
    }
    @ApiOperation(value = "查看设备交易分账统计", httpMethod = "GET")
    @RequestMapping(value = "/queryTerminalSplitSum", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo queryTerminalSplitSum(HttpServletRequest request, IotTerminalAddDTO iotTerminalAddDTO) {
        try{
            Customer customer = getLoginCustomer(request);
            if(IotType.IOT_CHARG_LINE.eq(iotTerminalAddDTO.getIotType())){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("successCount", iotChargingLineTransOrderService.getSuccessCount(iotTerminalAddDTO.getTerminalId()));
                jsonObject.put("successTransAmt", iotChargingLineTransOrderService.getSuccessTransAmt(iotTerminalAddDTO.getTerminalId()));
                jsonObject.put("totalSplitAmt", iotChargingLineTransOrderService.getTotalSplitAmt(iotTerminalAddDTO.getTerminalId(),customer.getId()));
                /*Page<IotChargingLineTransOrder> page = new Page<>(currentPage, pageSize);
                IPage<IotChargingLineTransOrder> pageList = iotChargingLineTransOrderService.page(page,
                        Wraps.<IotChargingLineTransOrder>lbQ().eq(IotChargingLineTransOrder::getTerminalId,iotTerminalAddDTO.getTerminalId())
                                .eq(IotChargingLineTransOrder::getStatus,2).orderByDesc(IotChargingLineTransOrder::getId));
                return RespVo.success(pageList);*/
                return RespVo.success(jsonObject);
            }
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("查询设备收费列表失败");
        }
        return RespVo.fail("物联设备类型不可为空");
    }
    @ApiOperation(value = "关联设备", httpMethod = "POST")
    @RequestMapping(value = "/addTerminal", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody

    public RespVo addTerminal(HttpServletRequest request, IotTerminalAddDTO iotTerminalAddDTO) {
        try{
            Customer customer = getLoginCustomer(request);
            IotTerminalInfo iotTerminalInfo = iotTerminalInfoService.getOne(
                    Wraps.<IotTerminalInfo>lbQ().eq(IotTerminalInfo::getTerminalId, iotTerminalAddDTO.getTerminalId())
                    .eq(IotTerminalInfo::getIotType, iotTerminalAddDTO.getIotType())
            );
            if (iotTerminalInfo == null) {
                throw new BizException("终端未找到");
            }
            if (iotTerminalInfo.getActiveStatus() == 1) {
                throw new BizException("终端已被绑定");
            }
            if(iotTerminalInfo.getCustomerId() != null && !iotTerminalInfo.getTerminalId().equals(customer.getId())){
                throw new BizException("终端已被其他服务商绑定");
            }
            IotChargingLineInfo iotChargingLineInfo = iotChargingLineInfoService.getOne(Wraps.<IotChargingLineInfo>lbQ().eq(IotChargingLineInfo::getTerminalId,iotTerminalInfo.getTerminalId()));
            iotChargingLineScaleService.initSale(iotChargingLineInfo.getId());
            //iotChargingLineInfo.setActiveStatus(1);
            iotTerminalInfo.setCustomerId(customer.getId());
            iotTerminalInfoService.updateById(iotTerminalInfo);
            return RespVo.success();
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("添加设备失败");
        }
    }
    @ApiOperation(value = "取消关联设备", httpMethod = "POST")
    @RequestMapping(value = "/deleteTerminal", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody

    public RespVo deleteTerminal(HttpServletRequest request, IotTerminalAddDTO iotTerminalAddDTO) {
        try{
            Customer customer = getLoginCustomer(request);
            IotTerminalInfo iotChargingLineInfo = iotTerminalInfoService.getOne(
                    Wraps.<IotTerminalInfo>lbQ().eq(IotTerminalInfo::getTerminalId, iotTerminalAddDTO.getTerminalId())
                            .eq(IotTerminalInfo::getIotType, iotTerminalAddDTO.getIotType())
            );
            if (iotChargingLineInfo == null) {
                throw new BizException("终端未找到");
            }
            iotChargingLineInfo.setActiveStatus(0);
            iotChargingLineInfo.setCustomerId(null);
            iotTerminalInfoService.updateById(iotChargingLineInfo);
            return RespVo.success();
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("取消关联设备失败");
        }
    }
    @ApiOperation(value = "查询设备", httpMethod = "POST")
    @RequestMapping(value = "/iotTerminalList", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody

    public RespVo iotTerminalList(HttpServletRequest request, @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, IotType iotType) {
        try{
            Customer customer = getLoginCustomer(request);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("iotTerminalInfoList", iotTerminalInfoService.pageByCustomer(iotType,currentPage,pageSize,customer.getId()));
            return RespVo.success(jsonObject);
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("查询设备失败");
        }
    }
    @ApiOperation(value = "查询设备关联商家", httpMethod = "POST")
    @RequestMapping(value = "/iotTerminalMerchantList", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody

    public RespVo iotTerminalMerchantList(HttpServletRequest request, @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, IotType iotType) {
        try{

            Customer customer = getLoginCustomer(request);
            List<IotTerminalInfo> iotTerminalInfos = iotTerminalInfoService.list(Wraps.<IotTerminalInfo>lbQ().eq(IotTerminalInfo::getCustomerId, customer.getId()));
            List<Long> merchantIds = new ArrayList<>();
            for(IotTerminalInfo iotTerminalInfo: iotTerminalInfos){
                if(iotTerminalInfo.getMerchantId() != null){
                    merchantIds.add(iotTerminalInfo.getMerchantId());
                }
            }
            if(merchantIds == null || merchantIds.size() <= 0){
                return RespVo.success(null);
            }
            JSONObject jsonObject = new JSONObject();
            Page<Merchant> page = new Page<>(currentPage, pageSize);
            IPage<Merchant> pageList = merchantService.page(page,Wraps.<Merchant>lbQ().in(Merchant::getId, merchantIds));
            for(Merchant merchant : pageList.getRecords()){
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("terminalCount", iotTerminalInfoService.count(Wraps.<IotTerminalInfo>lbQ().eq(IotTerminalInfo::getCustomerId,customer.getId()).eq(IotTerminalInfo::getMerchantId, merchant.getId())));
                jsonObject1.put("lastDateIncome", 0); //昨日收益
                jsonObject1.put("last7DaysIncome", 0); //近七天收益
                jsonObject1.put("totalIncome", iotChargingLineTransOrderService.getTotalSplitAmtAndMerchantId(merchant.getId(),customer.getId()));
                merchant.setJsonObject(jsonObject1);
            }
            jsonObject.put("merchantList", pageList);
            return RespVo.success(jsonObject);
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return RespVo.fail("查询设备失败");
        }
    }


    @ApiOperation(value = "关联商家", httpMethod = "POST")
    @RequestMapping(value = "/linkMerchant", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo linkMerchant(HttpServletRequest request, IotChargingLineLinkMerchantDTO iotChargingLineScaleSaveDTO) {
        try{
            if(iotChargingLineScaleSaveDTO.getTerminalId() == null){
                throw new BizException("终端号不可为空");
            }
            IotTerminalInfo iotTerminalInfo = iotTerminalInfoService.getOne(Wraps.<IotTerminalInfo>lbQ().eq(IotTerminalInfo::getTerminalId,iotChargingLineScaleSaveDTO.getTerminalId()));
            if(iotTerminalInfo == null){
                throw new BizException("终端号不存在");
            }
            iotTerminalInfo.setMerchantId(iotChargingLineScaleSaveDTO.getMerchantId());
            iotTerminalInfo.setActiveStatus(1);
            iotTerminalInfo.setSplit(100L);
            iotTerminalInfoService.updateById(iotTerminalInfo);
            return RespVo.success();
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("添加设备失败");
        }
    }
    @ApiOperation(value = "取消关联商家", httpMethod = "POST")
    @RequestMapping(value = "/unlinkMerchant", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo unlinkMerchant(HttpServletRequest request, IotChargingLineLinkMerchantDTO iotChargingLineScaleSaveDTO) {
        try{
            if(iotChargingLineScaleSaveDTO.getTerminalId() == null){
                throw new BizException("终端号不可为空");
            }
            IotTerminalInfo iotTerminalInfo = iotTerminalInfoService.getOne(Wraps.<IotTerminalInfo>lbQ().eq(IotTerminalInfo::getTerminalId,iotChargingLineScaleSaveDTO.getTerminalId()));
            if(iotTerminalInfo == null){
                throw new BizException("终端号不存在");
            }
            iotTerminalInfo.setMerchantId(null);
            iotTerminalInfo.setActiveStatus(0);
            iotTerminalInfoService.updateById(iotTerminalInfo);
            return RespVo.success();
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("添加设备失败");
        }
    }

    @ApiOperation(value = "关联公海商家申请", httpMethod = "POST")
    @RequestMapping(value = "/applyLinkPublicMerchant", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo applyLinkPublicMerchant(HttpServletRequest request, IotChargingLineLinkPublicMerchantDTO iotChargingLineScaleSaveDTO) {
        try{
            if(iotChargingLineScaleSaveDTO.getTerminalId() == null){
                throw new BizException("终端号不可为空");
            }
            if(iotChargingLineScaleSaveDTO.getSplit() == null){
                throw new BizException("配比不可为空");
            }
            if(iotChargingLineScaleSaveDTO.getSplit().compareTo(100L) > 0 || iotChargingLineScaleSaveDTO.getSplit().compareTo(0L) < 0){
                throw new BizException("配比设置在0-100间");
            }
            IotTerminalInfo iotTerminalInfo = iotTerminalInfoService.getOne(Wraps.<IotTerminalInfo>lbQ().eq(IotTerminalInfo::getTerminalId,iotChargingLineScaleSaveDTO.getTerminalId()));
            if(iotTerminalInfo == null){
                throw new BizException("终端号不存在");
            }
            Merchant merchant = merchantService.getById(iotChargingLineScaleSaveDTO.getMerchantId());
            if(merchant == null){
                throw new BizException("商家未找到");
            }
            if(merchant.getIsPublic() != 1){
                throw new BizException("该商家不是公海商家");
            }

            iotTerminalInfo.setMerchantId(iotChargingLineScaleSaveDTO.getMerchantId());
            iotTerminalInfo.setStatus(1); //绑定申请中
            iotTerminalInfo.setSplit(iotChargingLineScaleSaveDTO.getSplit());
            iotTerminalInfoService.updateById(iotTerminalInfo);
            return RespVo.success();
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("添加设备失败");
        }
    }

    @ApiOperation(value = "关联公海商家申请查询", httpMethod = "POST")
    @RequestMapping(value = "/applyLinkPublicMerchantQuery", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo applyLinkPublicMerchantQuery(HttpServletRequest request,@RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,@RequestParam(name = "type" ,defaultValue  = "0") Integer type) {
        try{
            if(type == null){
                throw new BizException("type不可为空");
            }
            log.info("关联公海商家申请查询： type {}", type);
            Customer customer = getLoginCustomer(request);

            List<Merchant> merchantList = merchantService.list(Wraps.<Merchant>lbQ().eq(Merchant::getIsPublic, 1).eq(Merchant::getCustomerId, customer.getId()));
            List<Long> merchIds = new ArrayList<>();
            for(Merchant merchant : merchantList){
                merchIds.add(merchant.getId());
            }
            Page<IotTerminalInfo> page = new Page<>(currentPage, pageSize);
            IPage<IotTerminalInfo> pageList = null;
            //type = 0
            //type = 1
            if(merchIds.isEmpty()){
                pageList = iotTerminalInfoService.page(page,Wraps.<IotTerminalInfo>lbQ().eq(IotTerminalInfo::getStatus,-1).orderByDesc(IotTerminalInfo::getId));
            }if(type.intValue() == 0){
                pageList = iotTerminalInfoService.page(page, Wraps.<IotTerminalInfo>lbQ().eq(IotTerminalInfo::getStatus,1).in(IotTerminalInfo::getMerchantId,merchIds).orderByDesc(IotTerminalInfo::getId));
            }else if(type.intValue() == 1){
                pageList = iotTerminalInfoService.page(page, Wraps.<IotTerminalInfo>lbQ().ne(IotTerminalInfo::getStatus,1).in(IotTerminalInfo::getMerchantId,merchIds).orderByDesc(IotTerminalInfo::getId));
            }
            for(IotTerminalInfo iotTerminalInfo : pageList.getRecords()){
                iotTerminalInfo.setMerchantFlag(false);
                iotTerminalInfo.setCustomerFlag(false);
                iotTerminalInfo.setSelfFlag(false);
                if(iotTerminalInfo.getMerchantId() != null){
                    iotTerminalInfo.setMerchant(merchantService.getById(iotTerminalInfo.getMerchantId()));
                    iotTerminalInfo.setMerchantFlag(true);
                }
                if(iotTerminalInfo.getCustomerId() != null){
                    iotTerminalInfo.setCustomer(customerService.getById(iotTerminalInfo.getCustomerId()));
                    iotTerminalInfo.setCustomerFlag(true);
                    if(customer.getId().equals(iotTerminalInfo.getCustomer().getId())){
                        iotTerminalInfo.setSelfFlag(true);
                    }
                }

            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("iotTerminalInfoList", pageList);
            return RespVo.success(jsonObject);
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("关联公海商家申请查询失败");
        }
    }
    @ApiOperation(value = "同意关联", httpMethod = "POST")
    @RequestMapping(value = "/confirmLinkPublicMerchant", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo confirmLinkPublicMerchant(HttpServletRequest request, IotChargingLineLinkPublicMerchantDTO iotChargingLineLinkPublicMerchantDTO) {
        try{
            if(iotChargingLineLinkPublicMerchantDTO.getTerminalId() == null){
                throw new BizException("终端号不可为空");
            }
            //IotTerminalInfo iotTerminalInfo = iotTerminalInfoService.getById(iotChargingLineLinkPublicMerchantDTO.getTerminalId());
            IotTerminalInfo iotTerminalInfo = iotTerminalInfoService.getOne(Wraps.<IotTerminalInfo>lbQ().eq(IotTerminalInfo::getTerminalId,iotChargingLineLinkPublicMerchantDTO.getTerminalId()));

            if(iotTerminalInfo == null){
                throw new BizException("终端号不存在");
            }
            Merchant merchant = merchantService.getById(iotChargingLineLinkPublicMerchantDTO.getMerchantId());
            if(merchant == null){
                throw new BizException("商家未找到");
            }
            if(merchant.getIsPublic() != 1){
                throw new BizException("该商家不是公海商家");
            }
            Customer customer = getLoginCustomer(request);
            if(!merchant.getCustomerId().equals(customer.getId())){
                throw new BizException("该商家不属于自己");
            }
            if(iotChargingLineLinkPublicMerchantDTO.getConfirmFlag() == 1){
                iotTerminalInfo.setMerchantId(iotChargingLineLinkPublicMerchantDTO.getMerchantId());
                iotTerminalInfo.setStatus(2); //绑定申请中
                iotTerminalInfo.setActiveStatus(1);
            }else {
                iotTerminalInfo.setStatus(0); //绑定申请中
                iotTerminalInfo.setMerchantId(null);
            }
            iotTerminalInfoService.updateById(iotTerminalInfo);
            return RespVo.success();
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("添加设备失败");
        }
    }
    @ApiOperation(value = "关联商家查询", httpMethod = "POST")
    @RequestMapping(value = "/linkMerchantQuery", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo linkMerchantQuery(HttpServletRequest request, IotChargingLineLinkMerchantDTO iotChargingLineScaleSaveDTO) {
        try{
            if(iotChargingLineScaleSaveDTO.getTerminalId() == null){
                throw new BizException("终端号不可为空");
            }
            IotTerminalInfo iotTerminalInfo = iotTerminalInfoService.getById(iotChargingLineScaleSaveDTO.getTerminalId());
            if(iotTerminalInfo == null){
                throw new BizException("终端号不存在");
            }
            if(iotTerminalInfo.getMerchantId() == null){
                throw new BizException("该商户管理未关联");
            }
            Merchant merchant = merchantService.getById(iotTerminalInfo.getMerchantId());
            return RespVo.success(merchant);
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("添加设备失败");
        }
    }
    @ApiOperation(value = "单设备收益汇总查询", httpMethod = "POST")
    @RequestMapping(value = "/terminalSplitSummery", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo terminalSplitSummery(HttpServletRequest request,IotTerminalAddDTO iotTerminalAddDTO) {
        try{
            Customer customer = getLoginCustomer(request);
            IotTerminalInfo iotTerminalInfo = iotTerminalInfoService.getOne(
                    Wraps.<IotTerminalInfo>lbQ().eq(IotTerminalInfo::getTerminalId, iotTerminalAddDTO.getTerminalId())
                            .eq(IotTerminalInfo::getIotType, iotTerminalAddDTO.getIotType())
            );
            JSONObject jsonObject = new JSONObject();

            //jsonObject.put("totalIncome", 0); //总收益
            jsonObject.put("lastDateIncome", 0); //昨日收益
            jsonObject.put("last7DaysIncome", 0); //近七天收益
            jsonObject.put("totalIncome", iotChargingLineTransOrderService.getTotalSplitAmt(iotTerminalAddDTO.getTerminalId(),customer.getId()));

            return RespVo.success(jsonObject);
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("添加设备失败");
        }
    }
    @ApiOperation(value = "汇总查询", httpMethod = "POST")
    @RequestMapping(value = "/totalSummery", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo totalSummery(HttpServletRequest request) {
        try{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("sumTerminal", 0); //设备总数
            jsonObject.put("validTerminal", 0); //已经安装
            jsonObject.put("sumMerchant", 0); //已经绑定商户
            jsonObject.put("activeTerminal", 0); //激活设备
            jsonObject.put("activeTerminalRate", 0); //激活设备占比

            jsonObject.put("totalIncome", 0); //总收益
            jsonObject.put("lastDateIncome", 0); //昨日收益
            jsonObject.put("last7DaysIncome", 0); //近七天收益
            jsonObject.put("withdrawalMoney", 0); //可提现金额

            jsonObject.put("platformIotTypeSum", 0); //平台设备总分类数
            jsonObject.put("IotTypeSum", 0); // 已拥有设备分类数
            jsonObject.put("IotTypeRate", 0); //占比
            TerminalSummeyDto terminalSummeyDto = new TerminalSummeyDto();
            terminalSummeyDto.setIotType(IotType.IOT_CHARG_LINE);
            terminalSummeyDto.setIotProfitAmt(0L);
            terminalSummeyDto.setIotProfitAmtRate(100L);
            terminalSummeyDto.setIotTransAmt(100L);
            terminalSummeyDto.setIotTransAmtRate(100L);
            List<TerminalSummeyDto> terminalSummeyDtos = new ArrayList<>();
            terminalSummeyDtos.add(terminalSummeyDto);
            jsonObject.put("terminalSummey", terminalSummeyDtos);

            return RespVo.success(jsonObject);
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("添加设备失败");
        }
    }
}
